//
//  FeatureInfoViewController.m
//  Korea
//
//  Created by 崔剑 on 15/9/8.
//  Copyright (c) 2015年 KoreaGo. All rights reserved.
//

#import "FeatureInfoViewController.h"
#import "NewInfoViewController.h"
// ThirdPart
#import "Header.h"
// UITableViewCell
#import "BestNewCell.h"
// 类
#import "Recommend.h"



@interface FeatureInfoViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *bestHotTableView;
@property (nonatomic, strong) NSMutableArray *sourceArr;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation FeatureInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"当天最热";
    self.view.backgroundColor = [UIColor colorWithRGB:0xf3f4f8 alpha:1];

    self.bestHotTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 113) style:UITableViewStylePlain];
    self.bestHotTableView.backgroundColor = [UIColor colorWithRGB:0xf3f4f8 alpha:1];
    
    // 隐藏滑动条
    self.bestHotTableView.showsVerticalScrollIndicator = NO;
    self.bestHotTableView.delegate = self;
    self.bestHotTableView.dataSource = self;
    [self.view addSubview:self.bestHotTableView];
    // 自适应屏幕
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // 注册重用池
    [self.bestHotTableView registerClass:[BestNewCell class] forCellReuseIdentifier:@"bestHotReuse"];
    
    //下拉刷新
    [self.bestHotTableView addHeaderWithTarget:self action:@selector(handleData)];
    [self.bestHotTableView headerBeginRefreshing];
    
    // 返回键
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
}

// 网络请求
- (void)handleData
{
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //常用的设置
    //小矩形的背景色
    self.HUD.color = [UIColor blackColor];//这儿表示无背景
    //显示的文字
    self.HUD.labelText = @"加载中";
    self.HUD.dimBackground = YES;
    
    
    NSString *str = [NSString stringWithFormat:@"http://192.168.0.3:8081/rest/hotitem?page=%ld&rows=10", self.page + 1];
    
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [AFNTool get:str Body:nil HttpResult:JSON success:^(id result) {
        
        NSMutableArray * arr = [result objectForKey:@"rows"];
        self.sourceArr = [Recommend modelArrWithDics:arr];
        
        [self.bestHotTableView reloadData];
        [self.HUD hide:YES];
        
    } failure:^(NSError *error) {
        [self.HUD hide:YES];
        
        //        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"网络状态不佳" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        //        [self.view addSubview:alertView];
        //        [alertView show];
        
    }];
    // 结束刷新
    [self.bestHotTableView headerEndRefreshing];
}

// 分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sourceArr.count;
}

// 每个分区的header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 10;
    }
    return 10;
}



// 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击cell");
    NewInfoViewController *newInfoVC = [[NewInfoViewController alloc] init];
    
    Recommend *recommend = self.sourceArr[indexPath.section];
    NSString *linkStr = [NSString stringWithFormat:@"http://192.168.0.3:8081/rest/hotitem/desc/%@", recommend.recommendID];
    newInfoVC.linkStr = linkStr;
    newInfoVC.recommend = recommend;
    [self.navigationController pushViewController:newInfoVC animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // cell高度
    return self.view.height / 10 * 7;
    
}

#pragma mark UITabelViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BestNewCell *cell = [self.bestHotTableView dequeueReusableCellWithIdentifier:@"bestHotReuse"];
    // 不显示选中颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.recommend = self.sourceArr[indexPath.section];
    return cell;
}

- (void)leftAction:(UIBarButtonItem *)button
{
    // 返回
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
